package com.tencreat.common.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.entity.ImMessage;
import com.tencreat.common.entity.ImSession;
import com.tencreat.common.entity.ImSetting;
import com.tencreat.common.entity.ImSettingBackground;
import com.tencreat.common.entity.vo.UserSessionVo;
import com.tencreat.common.mapper.ImSessionMapper;
import com.tencreat.common.mapper.ImSettingMapper;
import com.tencreat.common.service.ImSessionService;
import com.tencreat.common.service.ImSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ImSettingServiceImpl extends ServiceImpl<ImSettingMapper, ImSetting> implements ImSettingService {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public ImSettingBackground getBackGroundSetting() {
        if(!redisTemplate.hasKey(ImSettingBackground.KEY)){
            ImSetting one = lambdaQuery().eq(ImSetting::getName, ImSettingBackground.KEY).one();
            redisTemplate.opsForValue().set(ImSettingBackground.KEY, one.getValue());
        }
        return JSONUtil.parse(redisTemplate.opsForValue().get(ImSettingBackground.KEY)).toBean(ImSettingBackground.class);
    }

    @Override
    public void updateBackGroundSetting(ImSettingBackground setting) {
        String jsonStr = JSONUtil.toJsonStr(setting);
        lambdaUpdate().eq(ImSetting::getName, ImSettingBackground.KEY)
                .set(ImSetting::getValue, jsonStr).update();
        redisTemplate.opsForValue().set(ImSettingBackground.KEY, setting);
    }
}
